package io.gps.datatransfer.utils;

import org.apache.mina.core.buffer.IoBuffer;

import java.util.Random;

/**
 * 字符操作工具类
 * @author bcs
 * @version 2.0, $Date:  2020-11-26 13:04:48 $
 */
public class ByteUtils {
    public static Random r = new Random(1);
    public static int getCheckCode(IoBuffer iob) {
        int pre = iob.get();
        while (iob.hasRemaining()) {
            pre ^= iob.get();
        }
        return pre & 0xff;
    }

    public static int check(IoBuffer iob, int len) {

        int pre = iob.get();
        for (int i = 1; i < len; i++) {
            pre ^= iob.get();
        }
        return pre & 0xff;
    }

    /**
     * 解析bcd码
     * @param bcd
     * @return String
     */
    public static String decodeBCD(int bcd){
        return decodeBCD(bcd, "");
    }
    /**
     * 解析bcd码
     * @param bcd
     * @param limit 分隔符
     * @return String
     */
    public static String decodeBCD(int bcd, String limit){

        return ((byte)((bcd& 0xf0)>>>4))+limit+((byte)(bcd& 0x0f));
    }

    public static String getTelphone(IoBuffer data){
        StringBuilder telphone = new StringBuilder();
        for(int i = 0;i<6;i++){
            telphone.append(decodeBCD(data.getUnsigned()));
        }
        return telphone.toString();
    }

    public static String getSouDataLog(IoBuffer iob) {
        StringBuilder outstr=new StringBuilder();
        byte[] sss=iob.array();
        for (int i = 0; i < sss.length; i++) {
            String outss=("000"+Integer.toHexString((sss[i]& 0xFF)).toUpperCase());
            outstr.append(outss.substring(outss.length()-2));
        }
        return outstr.toString();

    }


    /**
     * 骐达其他数据
     * @param newdata
     * @param olddate
     * @param obj
     */
    public static void extAbandon(IoBuffer newdata,IoBuffer olddate,byte[] obj){
        olddate.get(obj);
        newdata.put(obj);
    }



    public static void main(String[] args)
    {    

      for(int i=0 ; i<1000 ;  i++)
      {
        int ran1 = r.nextInt(5);
        System.out.println(ran1+"------");
      }
    }

    public static IoBuffer getMessage(IoBuffer data) throws Exception{
        IoBuffer buf = IoBuffer.allocate(15);
        buf.setAutoExpand(true);
        byte b;
        while(data.hasRemaining()){
            b = data.get();
            if(b==0x7d && data.hasRemaining()){
                b = data.get();
                if(b==0x02){
                    buf.put((byte)0x7e);
                }else if(b==0x01){
                    buf.put((byte)0x7d);
                }else{
                    throw new Exception("非法信息");
                }
            }else{
                buf.put(b);
            }
        }
        buf.shrink();  //释放无用资源
        buf.flip();
        return buf;
    }

    public static void getMessage(IoBuffer srcdata,IoBuffer buf) throws Exception{
        byte b;
        while(srcdata.hasRemaining()){
            b = srcdata.get();
            if(b==0x7d && srcdata.hasRemaining()){
                b = srcdata.get();
                if(b==0x02){
                    buf.put((byte)0x7e);
                }else if(b==0x01){
                    buf.put((byte)0x7d);
                }else{
                    throw new Exception("非法信息");
                }
            }else{
                buf.put(b);
            }
        }
        buf.shrink();  //释放无用资源
        buf.flip();
    }
}
